package org.bigjava.buff.user.web.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.bigjava.buff.common.util.JsonObjectUtil;
import org.bigjava.buff.common.util.NotEmptyVerificationUtil;
import org.bigjava.buff.common.util.Page;
import org.bigjava.buff.goods.bean.ExteriorAndPrice;
import org.bigjava.buff.goods.service.GoodsService;
import org.bigjava.buff.user.bean.Deal;
import org.bigjava.buff.user.bean.User;
import org.bigjava.buff.user.service.DealService;
import org.bigjava.buff.user.service.UserService;
import org.bigjava.buff.common.util.PwdEncryptionUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;
    @Autowired
    private DealService dealService;
    @Autowired
    private GoodsService goodsService;

    NotEmptyVerificationUtil util = new NotEmptyVerificationUtil();

    //默认主页
    private String page = "redirect:/index.jsp";


    //登陆请求
    @RequestMapping("/login")
    public String login(User user,HttpSession session){

        User u = userService.login(user);
        if(u != null ){
            //登录成功
            System.out.println("Login Succeed");
            session.setAttribute("userNow", u);

            //判断权限  user,null  admin,1
            if(u.getJurisdiction() == null)  return "redirect:/index.jsp";
            //管理员
            page =  "adminpage/BuffTerminal";
        }

        return page;

    }

    //用户中心 我的钱包
    @RequestMapping("/center")
    public String goUserCenterPage(){
        return "userpage/user_center";
    }

    //用户设置页面
    @RequestMapping("/setting")
    public String goUserSettingPage(){
        return "userpage/user_setting";
    }

    //我的信息页面
    @RequestMapping("/info")
    public String goUserInfoPage(){
        return "userpage/user_info";
    }

    //用户收藏页面
    @RequestMapping("/collect")
    public String goUserCollectPage(){
        return "userpage/user_collect";
    }

    //权益卡券页面
    @RequestMapping("/coupon")
    public String goUserCopuonPage(){
        return "userpage/user_coupon";
    }

    //联系客服页面
    @RequestMapping("/feedback")
    public String goUserFeedbackPage(){
        return "userpage/user_feedback";
    }

    //我的出售
    @RequestMapping("mysell")
    public String goUserSell(HttpServletRequest request,Integer pageNum){

        User user = (User)request.getSession().getAttribute("userNow");

        Page page = new Page();
        page.setPageNum(pageNum);
        page.setPageSize(5);

        List list = dealService.getGoodsInSell(user.getId(), 1);


        request.setAttribute("sellList",list);

        return "userpage/my_sell";
    }

    //我的求购
    @RequestMapping("mybuy")
    public String goUserBuy(){
        return "userpage/my_buy";
    }


    //我的库存页面
    @RequestMapping("/inventory")
    public String goMyInventory(HttpServletRequest request,Integer pageNum){

        User u = (User) request.getSession().getAttribute("userNow");


        Page page = new Page();
        page.setPageNum(pageNum);
        page.setPageSize(15);

        //用户库存中的饰品
        page = userService.getUserInventory(u.getId(), page);

        //通过用户id查询用户是否有在交易的物品
        List<Deal> userSellGoodslist = dealService.getGoodsInSell(u.getId(), 1);
        //用户库存
        List<ExteriorAndPrice> userInventoryList = page.getList();
        if(!userSellGoodslist.isEmpty()){
            for(Deal deal : userSellGoodslist){
                for(ExteriorAndPrice e : userInventoryList){
                    if(deal.getEp().getId() == e.getId()){
                        e.setDealState(1);
                    }
                }
            }
        }

        request.setAttribute("userGoodsList", page.getList());
        request.setAttribute("pageInfo", page);

        return "userpage/my_inventory";
    }

    //显示等待出售商品信息
    @RequestMapping("/showPutawayGoods")
    @ResponseBody
    public String showgoods(Integer[] goodsIds){

        List list = new ArrayList();
        for (Integer id : goodsIds) {
            list.add(goodsService.getGoodsInfo(id));
        }
        String data = JsonObjectUtil.getJson(list);

        return data;
    }

    //用户退出 清除session
    @RequestMapping("/logout")
    public String userExit(HttpSession session){
        //清除session
        session.invalidate();;
        return page;
    }

    //修改用户信息
    @RequestMapping("/updateUser")
    public String updateUser(User user, String oldPwd, String newPwd, HttpSession session) {

        //获取是修改密码还是修改其他信息
        boolean updateType = util.isEmptyPwd(oldPwd, newPwd);

        // true修改其他的用户信息  false修改密码
        if((updateType)){
            userService.updateUser(user);
            User u = (User) session.getAttribute("userNow");
            session.setAttribute("userNow",userService.getUserbyId(u.getId()));
            page = "userpage/user_setting";
        } else {
            //加密后的新密码
            String enNewPwd = PwdEncryptionUtil.shaEncode(newPwd);
            //加密后的原密码
            String enOldPwd = PwdEncryptionUtil.shaEncode(oldPwd);
            //获取当前登陆用户
            User u = (User)session.getAttribute("userNow");
            //如果用户输入的原密码与之前的的原密码一致  进行修改
            if(u.getPassword() == enOldPwd){
                user.setPassword(enNewPwd);
                userService.updateUser(user);
            }
            //清除session 重新登陆
            session.invalidate();;
        }

        return page;
    }

    //充值金额
    @RequestMapping("/topUp")
    public String topUpMoney(){
        return "userpage/user_center";
    }


}
